import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../controller/feedback_page.dart';
import '../../components/toast_utils.dart';
import '../controller/about_page.dart';
import '../../components/custom_dialog.dart';
import '../../manger/user_manger.dart';
import '../../main.dart';
import '../controller/logout_page.dart';

class SetPage extends StatelessWidget {
  const SetPage({super.key});

  List<Map<String, String>> setOptions(BuildContext context) {
    final localizations = AppLocalizations.of(context);
    if (localizations == null) return [];
    
    return [
      {"title": localizations.closeAccount, "name": "personadata"},

      // {"title": localizations.personadata, "name": "personadata"},
      {"title": localizations.feedback, "name": "Feedback"},
      // {"title": localizations.rating, "name": "Rating"},
      {"title": localizations.aboutUs, "name": "AboutUs"},
      {"title": localizations.clearCache, "name": "ClearCache"},
      {"title": localizations.loginout, "name": "personadata"},
    ];
  }

  @override
  Widget build(BuildContext context) {
    final localizations = AppLocalizations.of(context);
    if (localizations == null) return const SizedBox.shrink();
    
    final setOptionsList = setOptions(context);

    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 40,
        title: Text(localizations.set,style: const TextStyle(fontSize: 17)),
      ),
      body: ListView.builder(
          padding: const EdgeInsets.all(15),
          itemCount: setOptionsList.length,
          itemBuilder: (context, index) {
          final option = setOptionsList[index];

          return Container(
            height: 65,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: index == 0 
                ? BorderRadius.only(topLeft: Radius.circular(12), topRight: Radius.circular(12))
                : index == setOptionsList.length - 1
                  ? BorderRadius.only(bottomLeft: Radius.circular(12), bottomRight: Radius.circular(12))
                  : BorderRadius.zero,
            ),
            child: Column(
              children: [
                Expanded(
                  child: GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () {
                      if (index == 1) {
                        Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) =>  FeedbackPage()),
                        );
                      } else if (index == 2) {
                        Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) =>  AboutPage()),
                        );
                      } else if (index == 3) {
                        Future.delayed(Duration(seconds: 1), () {
                          ToastUtils.showCenterToast(context, AppLocalizations.of(context)!.clearCacheSuccess);
                        });
                      } else if (index == 4) {
                        showDialog(
                          context: context,
                          builder: (context) => CustomDialog(
                            title: localizations.isloginout,
                            leftButtonText: localizations.cancel,
                            rightButtonText: localizations.confirm,
                            onLeftPressed: () => Navigator.pop(context),
                            onRightPressed: () {
                              // TODO: 实现退出登录逻辑
                              UserInfos.shared.cleanUserInfos();
                              Navigator.pushReplacement(
                                context,
                                MaterialPageRoute(
                                  builder: (context) => MainPage(),
                                ),
                              );
                            },
                          ),
                        );
                      } else {
                        Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) =>  LogoutPage()),
                        );
                      }
                    },
                    child: Row(
                      children: [
                        SizedBox(width: 15),
                        Image.asset("assets/images/${option['name']}.png", width: 22, height: 22),
                        SizedBox(width: 15),
                        Expanded(child: Text(option['title']!)),
                        Image.asset("assets/images/mineicon.png", width: 14, height: 14),
                        SizedBox(width: 15),
                      ],
                    ),
                  ),
                ),
                if (index != setOptionsList.length - 1)
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 15),
                  child: const Divider(height: 1, color: Color(0xFFF5F5F5)),
                ),
              ],
            ),
          );
        },
      ),
    );  
  }
}